<template>
  <el-form ref="form" label-width="120px" :model="form" :rules="rules">
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
        <el-option v-for="item in employees" :key="item.id" :value="item.username" :label="item.username" />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="hCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getEmployeeSimple } from '@/api/employees'
import { addDepartments, getDepartDetail, updateDepartments } from '@/api/department.js'
export default {
  props: {
    id: {
      type: String,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    originList: {
      type: Array,
      required: true
    }
  },
  data() {
    // code自定义校验
    const validCode = (rule, value, callback) => {
      const { originList, id, isEdit } = this
      let codeList = originList.map(item => item.code)
      if (isEdit) {
        codeList = originList.filter(it => it.id !== id).map(item => item.code)
        console.log(codeList, 'codeList')
      }
      codeList.includes(value) ? callback(new Error(value + 'value被占用了')) : callback()
    }

    const validName = (rule, value, callback) => {
      const { originList, id, isEdit } = this
      let nameList = originList.filter(it => it.pid === id).map(item => item.name)
      if (isEdit) {
        // 1. 找到当前被编辑的元素
        // 2. 取出pid(它的父级是谁)
        // 3. 过滤： 同一个父级，不包括自己； map： 只要需要名字
        const obj = originList.find(it => it.id === id)
        const pid = obj.pid
        console.log(pid)
        nameList = originList.filter(it => it.pid === pid && it.id !== id).map(item => item.name)
        console.log(nameList, 789789)
      }
      nameList.includes(value) ? callback(new Error(value + '被占用了')) : callback()
    }
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      employees: [],
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { validator: validName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { validator: validCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '管理者不能为空', trigger: 'chenage' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.loadEmployee()
    this.loadDetail()
  },
  methods: {
    // 获取部门详情
    async loadDetail() {
      if (this.isEdit) {
        const res = await getDepartDetail(this.id)
        console.log('部门信息', res.data)
        this.form = res.data
      }
    },
    // 获取信息
    async loadEmployee() {
      console.log(2222, this.form)
      const res = await getEmployeeSimple()
      console.log('员工信息', res)
      this.employees = res.data
    },
    // 修改部门信息
    async doEdit() {
      try {
        const res = await updateDepartments(this.form)
        console.log(res)
        this.$emit('success')
      } catch (err) {
        console.log(err)
      }
    },
    hCancel() {
      this.$emit('close')
    },
    hSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.isEdit ? this.doEdit() : this.doAdd()
        }
      })
    },
    // 执行具体的添加动作
    async doAdd() {
      // 去获取pid： 当前要添加的部门所在的父级的id
      // 从props中获取
      // 1. 组装参数
      const params = { ...this.form, pid: this.id }
      console.log(params)
      // 2. 调用接口
      const rs = await addDepartments(params)
      console.log(rs)
      // 3. 通知父组件：关闭弹层，再次更新数据
      this.$emit('success')
    }
  }
}
</script>
